package com.cloud.spring.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class EmailService {
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String sender;

    /**
     * 发送普通文本邮件
     */
    public void sendSimpleMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(sender); //自己给自己发送邮件
        message.setSubject("主题：简单邮件");
        message.setText("这是发送的普通文本邮件");
        mailSender.send(message);
    }

    /**
     * 发送html格式的邮件
     *
     * @throws MessagingException
     */
    public void sendHtmlMail() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        mimeMessageHelper.setTo("1439097053@qq.com");
        mimeMessageHelper.setFrom(sender);
        mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试【HTML】");
        mimeMessageHelper.setText("<html lang=\"en\"><head> <meta charset=\"UTF-8\"> <title>Title</title> </head> <body> <h1>这是我发给你的邮件</h1> <p>鹅鹅鹅</p> <p>曲项向天歌</p> <p>白毛浮绿水</p> <p>红掌拨清波</p> <p>欲了解更多,请关注<a href=\"http://www.baidu.com\">百度一下</a></p></body></html>", true);
        // 发送邮件
        mailSender.send(mimeMessage);
    }

    /**
     * 发送包含图片的邮件
     *
     * @throws Exception
     */
    public void sendAttachedImageMail() throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // multipart模式
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setTo("1439097053@qq.com");
        mimeMessageHelper.setFrom(sender);
        mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试【图片】");
        StringBuilder sb = new StringBuilder();
        sb.append("<html><head></head>");
        sb.append("<body><h1>spring 邮件测试</h1><p>hello!this is spring mail test。</p>");
        // cid为固定写法，imageId指定一个标识
        sb.append("<img src=\"cid:imageId\"/></body>");
        sb.append("</html>");
        // 启用html
        mimeMessageHelper.setText(sb.toString(), true);
        // 设置imageId
        FileSystemResource img = new FileSystemResource(new File("D:/test.png"));
        mimeMessageHelper.addInline("imageId", img);
        // 发送邮件
        mailSender.send(mimeMessage);
    }

    /**
     * 发送带附件的html邮件
     *
     * @throws Exception
     */
    public void sendAttendedFileMail() throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // multipart模式
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        mimeMessageHelper.setTo("1439097053@qq.com");
        mimeMessageHelper.setFrom(sender);
        mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试【附件】");

        StringBuilder sb = new StringBuilder();
        sb.append("<html><head></head>");
        sb.append("<body><h1>spring 邮件测试</h1><p>hello!this is spring mail test。</p></body>");
        sb.append("</html>");

        // 启用html
        mimeMessageHelper.setText(sb.toString(), true);
        // 设置附件
        FileSystemResource img = new FileSystemResource(new File("D:/test.png"));
        mimeMessageHelper.addAttachment("test.png", img);

        // 发送邮件
        mailSender.send(mimeMessage);

        System.out.println("邮件已发送");
    }

}
